theta = [0.125 : 0.25 : 180.]';
phi   = [0.125 : 0.25 : 360.]';

File_1 = '/data/backup/mnair/ocean/tpxo72_720_1440_model_results/tides_M2_720_1440.res';

fid = fopen(File_1, 'r', 'n');

LBlock = fread(fid, 1, 'long');
NO = fread(fid, 1, 'long');
Period = fread(fid, 1, 'double'); 
np = fread(fid, 1, 'long');
nt = fread(fid, 1, 'long');
fseek(fid, LBlock*4, 'bof'); 

Hpr_0km_1 = fread(fid, [nt np], 'float')*4*pi*100.;
Hpi_0km_1 = fread(fid, [nt np], 'float')*4*pi*100.;


Htr_0km_1 = fread(fid, [nt np], 'float')*4*pi*100.;
Hti_0km_1 = fread(fid, [nt np], 'float')*4*pi*100.;


Hrr_0km_1 = fread(fid, [nt np], 'float')*4*pi*100.;
Hri_0km_1 = fread(fid, [nt np], 'float')*4*pi*100.;


% Epr_0km_1 = fread(fid, [nt np], 'float')*1.0E+06;
% Epi_0km_1 = fread(fid, [nt np], 'float')*1.0E+06;
% 
% 
% 
% Etr_0km_1 = fread(fid, [nt np], 'float')*1.0E+06;
% Eti_0km_1 = fread(fid, [nt np], 'float')*1.0E+06;


fclose(fid);

max_Hxr_0km_1 = max (max (abs(Htr_0km_1)));
max_Hxi_0km_1 = max (max (abs(Hti_0km_1)));

max_Hyr_0km_1 = max (max (abs(Hpr_0km_1)));
max_Hyi_0km_1 = max (max (abs(Hpi_0km_1)));

max_Hzr_0km_1 = max (max (abs(Hrr_0km_1)));
max_Hzi_0km_1 = max (max (abs(Hri_0km_1)));

%%
r = 6371.2E+3

map = [    0.2510         0    0.2510
    0.2331         0    0.3045
    0.2151         0    0.3580
    0.1972         0    0.4115
    0.1793         0    0.4650
    0.1613         0    0.5185
    0.1434         0    0.5720
    0.1255         0    0.6255
    0.1076         0    0.6790
    0.0896         0    0.7325
    0.0717         0    0.7860
    0.0538         0    0.8395
    0.0359         0    0.8930
    0.0179         0    0.9465
         0         0    1.0000
    0.0091    0.0909    1.0000
    0.0182    0.1818    1.0000
    0.0273    0.2727    1.0000
    0.0364    0.3636    1.0000
    0.0455    0.4545    1.0000
    0.0545    0.5455    1.0000
    0.0636    0.6364    1.0000
    0.0727    0.7273    1.0000
    0.0818    0.8182    1.0000
    0.0909    0.9091    1.0000
    0.1000    1.0000    1.0000
    0.2500    1.0000    1.0000
    0.4000    1.0000    1.0000
    0.5500    1.0000    1.0000
    0.7000    1.0000    1.0000
    0.8500    1.0000    1.0000
    1.0000    1.0000    1.0000
    1.0000    1.0000    0.8333
    1.0000    1.0000    0.6667
    1.0000    1.0000    0.5000
    1.0000    1.0000    0.3333
    1.0000    1.0000    0.1667
    1.0000    1.0000         0
    1.0000    0.9333         0
    1.0000    0.8667         0
    1.0000    0.8000         0
    1.0000    0.7333         0
    1.0000    0.6667         0
    1.0000    0.6000         0
    1.0000    0.5333         0
    1.0000    0.4667         0
    1.0000    0.4000         0
    1.0000    0.3333         0
    1.0000    0.2667         0
    1.0000    0.2000         0
    1.0000    0.1333         0
    1.0000    0.0667         0
    1.0000         0         0
    1.0000         0    0.0909
    1.0000         0    0.1818
    1.0000         0    0.2727
    1.0000         0    0.3636
    1.0000         0    0.4545
    1.0000         0    0.5455
    1.0000         0    0.6364
    1.0000         0    0.7273
    1.0000         0    0.8182
    1.0000         0    0.9091
    1.0000         0    1.0000];
colormap(map);

set(gcf, 'PaperOrientation', 'Portrait', 'PaperType', 'A4', 'PaperUnits', ...
    'centimeters', 'PaperPosition', [0.2 0.2 20.5 15]); 

%%
load coast;

%plot the magnitude of Hr component


Hr_power = abs(complex(Hrr_0km_1,Hri_0km_1));

imagesc(phi, theta, (Hr_power), [0 2]);
hold on; plot([long; 360+long], 90-[lat; lat], '-w'); hold off; 
%colormap(map);

colorbar('h');
set(gca, 'Xtick', [0:60:360], 'YTick', [0:30:180]);
title 'Abs Bz; M2';
saveas(gcf, '/home/mnair/projects/tides/M2_Abs_Bz_720_1440_1km.png','png');

%subplot('Position', [0.07 0.55 0.40 0.40]);
imagesc(phi, theta, -Hrr_0km_1, [-10 10]);
hold on; plot([long; 360+long], 90-[lat; lat], '-k'); hold off; 
colormap(map);

colorbar('h');
set(gca, 'Xtick', [0:60:360], 'YTick', [0:30:180]);
title 'Re Bz; M2';
saveas(gcf, '/home/mnair/projects/tides/M2_Re_Bz_720_1440_1km.png','png');

%subplot('Position', [0.07  0.05 0.40 0.40]);
imagesc(phi, theta, -Hri_0km_1, [-5 5]);
hold on; plot([long; 360+long], 90-[lat; lat], '-k'); hold off; 
colormap(map);
colorbar('h');
set(gca, 'Xtick', [0:60:360], 'YTick', [0:30:180]);
title 'Im Bz; M2';
saveas(gcf, '/home/mnair/projects/tides/N2_Im_Bz_720_1440_1km.png','png');


%subplot('Position', [0.56  0.05 0.40 0.40]);
imagesc(phi, theta, -Htr_0km_1, [-5 5] );
hold on; plot([long; 360+long], 90-[lat; lat], '-k'); hold off
colormap(map);
colorbar('h');
set(gca, 'Xtick', [0:60:360], 'YTick', [0:30:180]);
title '(d) Re Hx; M2';
saveas(gcf, '/home/mnair/projects/tides/N2_Re_Hx_720_1440_1km.png','png');

%subplot('Position', [0.56  0.05 0.40 0.40]);
imagesc(phi, theta, -Hti_0km_1, [-5 5] );
hold on; plot([long; 360+long], 90-[lat; lat], '-k'); hold off
colormap(map);
colorbar('h');
set(gca, 'Xtick', [0:60:360], 'YTick', [0:30:180]);
title '(e) Im Hx; M2';
saveas(gcf, '/home/mnair/projects/tides/M2_Im_Hx_720_1440_1km.png','png');

%subplot('Position', [0.56  0.05 0.40 0.40]);
imagesc(phi, theta, Hpr_0km_1, [-5 5] );
hold on; plot([long; 360+long], 90-[lat; lat], '-k'); hold off
colormap(map);
colorbar('h');
set(gca, 'Xtick', [0:60:360], 'YTick', [0:30:180]);
title 'Re Hy; M2';
saveas(gcf, '/home/mnair/projects/tides/N2_Re_Hy_720_1440_1km.png','png');

%subplot('Position', [0.56  0.05 0.40 0.40]);
imagesc(phi, theta, Hpi_0km_1, [-5 5] );
hold on; plot([long; 360+long], 90-[lat; lat], '-k'); hold off
colormap(map);
colorbar('h');
set(gca, 'Xtick', [0:60:360], 'YTick', [0:30:180]);
title 'Im Hy; M2';
saveas(gcf, '/home/mnair/projects/tides/N2_Im_Hy_720_1440_1km.png','png');


%print -dpng  'M2_Hx_1km_sedi_5000S_vs_no_sedi-sedi_5000S.png';

return


%% Downsample the 720x1440 data to 180x360

% lat_orig = repmat([-89.8750:0.25:89.875]',[1,1440]);
% lon_orig = repmat([0.125:0.25:359.875],[720,1]);
% 
% latmat = repmat([-89.5:1:89.5]',[1,360]);
% lonmat = repmat( [0.5:1:359.5],[180,1]);
